


//
//  LocationNotification.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 2016/9/27.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "LocationNotification.h"


#define LocalNotification_ScheduleTitle_KEY @"scheduleTitle"
#define LocalNotification_ScheduleStartTime_KEY @"scheduleStartTime"
#define LocalNotification_ScheduleStartDate_KEY @"scheduleStartDate"


@implementation LocationNotification

#pragma mark - 初始化

+ (void)setDefaultData {
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; //清空本地通知
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    //    [USER_DEFAULT setObject:[NSNumber numberWithBool:YES] forKey:HAVE_BEEN_USED];
    //    [USER_DEFAULT setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:SYSTEM_SHORT_VERSION] forKey:LASTVERSION];
    //    [USER_DEFAULT setObject:array forKey:RINGS];
    //    [USER_DEFAULT synchronize];
}


#pragma mark - 删除指定本地通知

+ (void)cancelLocalNotificationWithIdentifier:(NSString *)identifier {
    
    NSArray<UILocalNotification *> *lns = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for (UILocalNotification *ln in lns) {
        
        if ([[ln.userInfo objectForKey:@"identifier"] isEqualToString:identifier]) {
            [[UIApplication sharedApplication] cancelLocalNotification:ln];
            break;
        }
    }
}

#pragma mark - 删除所有本地通知

+ (void)cancelAllLocalNotifications{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

#pragma mark - 创建本地通知

+ (UILocalNotification *)registerLocalNotificationWithFireDate:(NSDate *)firedate
                                                    repeatType:(NSCalendarUnit)repeattype
                                                        entity:(NSDictionary *)dict {
    UILocalNotification *ln = [LocationNotification registerALocalNotificationWithFireDate:firedate repeatType:repeattype Seq:@"0" entity:dict];
    return ln;
}

+ (UILocalNotification *)registerALocalNotificationWithFireDate:(NSDate *)firedate
                                                    repeatType:(NSCalendarUnit)repeattype
                                                           Seq:(NSString *)sequence
                                                        entity:(NSDictionary *)dict {
    
    UILocalNotification *localNoti = [[UILocalNotification alloc] init];
    localNoti.repeatInterval = 0;
    localNoti.fireDate = firedate;
    localNoti.soundName = UILocalNotificationDefaultSoundName;
    NSString *title = [dict objectForKey:LocalNotification_ScheduleTitle_KEY];
    NSString *body = [dict objectForKey:LocalNotification_ScheduleStartTime_KEY];
    localNoti.alertAction = @"查看";
    localNoti.alertBody = body;
    localNoti.alertTitle = title;
//    localNoti.applicationIconBadgeNumber = 1;
    [UIApplication sharedApplication].applicationIconBadgeNumber++;
    localNoti.userInfo = dict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
    
    return localNoti;
}


#pragma mark - 推迟本地通知

+ (UILocalNotification *)postPoneLocalNoticationWithFireDate:(NSDate *)firedate
                                 repeatType:(NSCalendarUnit)repeattype
                                        Seq:(NSString *)sequence
                                     entity:(NSDictionary *)dict {
    
    UILocalNotification *postPoneLn = [LocationNotification registerALocalNotificationWithFireDate:firedate repeatType:repeattype Seq:sequence entity:dict];
    
    return postPoneLn;
}

+ (void)testNotification {
    
    UILocalNotification *localNoti =
    [UILocalNotification new];
    localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    localNoti.alertBody = @"测试本地通知111";
    localNoti.alertTitle = @"本地通知111";
    localNoti.alertAction = @"打开111";
    localNoti.soundName = UILocalNotificationDefaultSoundName;
    localNoti.repeatInterval = NSCalendarUnitMinute;
    //    localNoti.repeatCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
    localNoti.applicationIconBadgeNumber = 123;
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    userInfo[@"name"] = @"haha111";
    localNoti.userInfo = userInfo;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
    
    UILocalNotification *localNoti2 =
    [UILocalNotification new];
    localNoti2.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    localNoti2.alertBody = @"测试本地通知222";
    localNoti2.alertTitle = @"本地通知222";
    localNoti2.alertAction = @"打开2222";
    localNoti2.soundName = UILocalNotificationDefaultSoundName;
    localNoti.repeatInterval = NSCalendarUnitMinute;
    localNoti.repeatCalendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
    localNoti2.applicationIconBadgeNumber = 123;
    NSMutableDictionary *userInfo2 = [NSMutableDictionary dictionary];
    userInfo[@"name"] = @"haha222";
    localNoti2.userInfo = userInfo2;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti2];
    
}


@end
